import axios from 'axios';
import { API_GAME_URL, API_V1_URL } from '@/config/api';
import { AnyObject, HttpResponse, PageResponse } from '@/types/global';
import {
  Game,
  GameEnrollType,
  GameGroup,
  GamePlay,
  GamePlayResult,
  GameRankDataGroup,
  GameSignImportItem,
  GameUserMatch,
  HandicapPureSoreRankUser,
  HandicapTotalScoreRankUser,
} from '@/types/game';
import { CourseItem, CourseHole } from '@/types/course';
import { User } from '@/types/user';
import { PlayerScore } from '@/types/game-score';

export function createGame(data: AnyObject) {
  return axios.post<HttpResponse<Game>>(`${API_GAME_URL}`, data);
}

export function passGame(id: string) {
  return axios.post<HttpResponse<Game>>(`${API_GAME_URL}/pass/${id}`);
}

export function refuseGame(id: string, data: AnyObject) {
  return axios.post<HttpResponse<Game>>(`${API_GAME_URL}/refuse/${id}`, data);
}

export function publishGame(id: string) {
  return axios.post<HttpResponse<Game>>(`${API_GAME_URL}/publish/${id}`);
}

export function updateGame(data: AnyObject) {
  return axios.put<HttpResponse<Game>>(`${API_GAME_URL}`, data);
}

export function deleteGame(id: string) {
  return axios.delete(`${API_GAME_URL}/${id}`);
}

export function notifySignGame(id: string) {
  return axios.post<HttpResponse<Game>>(`${API_GAME_URL}/notify/sign/${id}`);
}

export function getGameDetail(id: string) {
  return axios.get<HttpResponse<Game>>(`/api/v1/game/detail?id=${id}`);
}

export function getGameList(data: AnyObject) {
  return axios.post<HttpResponse<PageResponse<Game>>>(
    `${API_GAME_URL}/page`,
    data
  );
}

export function getCourseList(data: AnyObject) {
  return axios.post<HttpResponse<PageResponse<CourseItem>>>(
    '/api/v1/course/page',
    data
  );
}

export function getGamePlayerList(data: AnyObject) {
  return axios.post<HttpResponse<PageResponse<User>>>(
    '/api/v1/game/enroll/user',
    data
  );
}

export function getGameEnrollTemplate(gameId: string) {
  return axios.post<HttpResponse<string>>(
    `/api/v1/admin/game/enroll/template/export?gameId=${gameId}`
  );
}

export function importGamePlayers(gameId: string, formData: FormData) {
  return axios.post<HttpResponse<GameSignImportItem[]>>(
    `/api/v1/admin/game/enroll/excel/import?gameId=${gameId}`,
    formData
  );
}

export function importGameCustomerPlayers(gameId: string, formData: FormData) {
  return axios.post<HttpResponse<GameSignImportItem[]>>(
    `/api/v1/admin/game/enroll/simple/excel/import?gameId=${gameId}`,
    formData
  );
}

export function exportGamePlayer(gameId: string) {
  return axios.post<HttpResponse<string>>(
    `/api/v1/admin/game/enroll/user/export?gameId=${gameId}`
  );
}

export function createUnrealPlayer(gameId: string, count: number) {
  return axios.post(
    `/api/v1/admin/game/user/gen/unreal?gameId=${gameId}&playerCount=${count}`
  );
}

export function getGameGroup(gameId: string) {
  return axios.post<HttpResponse<GameGroup[]>>(
    `/api/v1/admin/game/group/user`,
    {
      gameId,
    }
  );
}

export function createGameGroup(data: AnyObject) {
  return axios.post('/api/v1/admin/game/group/manual/add', data);
}

export function deleteGameGroup(id: string) {
  return axios.delete(`/api/v1/admin/game/group/manual/delete/${id}`);
}

export function updateGameGroup(data: AnyObject) {
  return axios.put('/api/v1/admin/game/group/manual/update', data);
}

export function getGameEnrollTypes(gameId: string) {
  return axios.get<HttpResponse<GameEnrollType[]>>(
    `/api/v1/admin/game/enroll-type/list?gameId=${gameId}`
  );
}

export function createGameEnrollType(data: AnyObject) {
  return axios.post(`/api/v1/admin/game/enroll-type`, data);
}

export function updateGameEnrollType(data: AnyObject) {
  return axios.put(`/api/v1/admin/game/enroll-type/${data.id}`, data);
}

export function deleteGameEnrollType(id: number, gameId: string) {
  return axios.delete(
    `/api/v1/admin/game/enroll-type/${id}?gameId=${gameId}&id=${id}`
  );
}

export function addPlayersToEnrollType(
  gameId: string,
  typeId: number,
  playerIds: string[]
) {
  return axios.post(
    `/api/v1/admin/game/enroll-type/user/add?gameId=${gameId}&typeId=${typeId}`,
    playerIds
  );
}

export function removePlayersFromEnrollType(
  gameId: string,
  typeId: number,
  playerIds: string[]
) {
  return axios.post(
    `/api/v1/admin/game/enroll-type/user/remove?gameId=${gameId}&typeId=${typeId}`,
    playerIds
  );
}

export function getPlayerIdsFromEnrollType(gameId: string, typeId: number) {
  return axios.get<HttpResponse<User[]>>(
    `/api/v1/admin/game/enroll-type/user/list?gameId=${gameId}&typeId=${typeId}`
  );
}

export function getPlayerNotEnrollType(gameId: string) {
  return axios.get<HttpResponse<User[]>>(
    `/api/v1/admin/game/enroll-type/user/not/list?gameId=${gameId}`
  );
}

export function getGameNotGroupUsers(gameId: string) {
  return axios.post<HttpResponse<User[]>>(
    `/api/v1/admin/game/not/group/user?gameId=${gameId}`
  );
}

export function addPlayerToGameGroup(data: AnyObject) {
  return axios.post('/api/v1/admin/group/user/manual/add', data);
}

export function removePlayerFromGameGroup(data: AnyObject) {
  return axios.delete('/api/v1/admin/group/user/manual/remove', {
    data,
  });
}

export function uploadGroupExcel(gameId: string, data: FormData) {
  return axios.post('/api/v1/admin/game/group/excel/import', data, {
    params: {
      gameId,
    },
  });
}

export function getPlayerScore(gameId: string) {
  return axios.get<HttpResponse<PlayerScore[]>>(
    `/api/v1/game/hole/score/outter/query?gameId=${gameId}`
  );
}

export function importPlayerHoleScores(gameId: string, data: FormData) {
  return axios.post(
    `${API_V1_URL}/game/hole/score/import?gameId=${gameId}`,
    data
  );
}

export function queryCourseHole(gameId: string) {
  return axios.get<HttpResponse<CourseHole[]>>(
    `/api/v1/course/game/hole/query/last?gameId=${gameId}`
  );
}

export function updatePlayerScore(data: AnyObject) {
  return axios.post(`${API_V1_URL}/game/hole/score/add/update`, data);
}

export function confirmPlayerTotalScore(data: AnyObject, params: AnyObject) {
  return axios.post(`${API_V1_URL}/game/total/score/update`, data, {
    params,
  });
}

export function confirmTotalScoreBatch(gameId: string) {
  return axios.post(
    `${API_V1_URL}/game/total/score/confirm/batch?gameId=${gameId}`
  );
}

export function getAllPlayTypes() {
  return axios.get<HttpResponse<GamePlay[]>>(
    '/api/v1/admin/game/play/type/list'
  );
}

export function addGamePlay(data: AnyObject) {
  return axios.post('/api/v1/admin/game/play/create', data);
}

export function deleteGamePlay(id: number) {
  return axios.delete(`/api/v1/admin/game/play/delete/${id}`);
}

export function clearGamePlay(gameId: string) {
  return axios.delete(`/api/v1/admin/game/play/clear?gameId=${gameId}`);
}

export function runGamePlay(id: number) {
  return axios.post(`/api/v1/admin/game/play/run/${id}`);
}

export function getGamePlayList(gameId: string) {
  return axios.get<HttpResponse<GamePlayResult[]>>(
    `/api/v1/game/play/list?gameId=${gameId}`
  );
}

export function getGamePlayDetail(id: number) {
  return axios.get<HttpResponse<GamePlayResult[]>>(
    `/api/v1/game/play/detail?id=${id}`
  );
}

export function clearGameCaddie(gameId: string) {
  return axios.delete(`/api/v1/admin/game/caddie/clear?gameId=${gameId}`);
}

export function mockGame(data: AnyObject) {
  return axios.post(`/api/v1/admin/game/mock`, data);
}

export function clearMockGame(parentGameId: string) {
  return axios.delete(`/api/v1/admin/game/mock?parentGameId=${parentGameId}`);
}

export function manualEnrollGame(data: AnyObject) {
  return axios.post('/api/v1/admin/game/enroll/manual', data);
}

export function deleteManualGameEnroll(data: AnyObject) {
  return axios.delete('/api/v1/admin/game/enroll/manual', {
    data,
  });
}

export function getGameUserMatchList(gameId: string) {
  return axios.get<HttpResponse<GameUserMatch[]>>(
    `/api/v1/admin/game/user/match?gameId=${gameId}`
  );
}

export function createGameUserMatch(gameId: string, data: string[]) {
  return axios.post(`/api/v1/admin/game/user/match?gameId=${gameId}`, data);
}

export function deleteGameUserMatch(gameId: string, matchTag: string) {
  return axios.delete(
    `/api/v1/admin/game/user/match?gameId=${gameId}&matchTag=${matchTag}`
  );
}

export function prepareGameRank(gameId: string, calType: number) {
  return axios.post(
    `/api/v1/admin/game/rank/prepare?gameId=${gameId}&calType=${calType}`
  );
}

export function flushGameRank(year: string, calType: number) {
  return axios.post(
    `/api/v1/admin/game/rank/run?year=${year}&calType=${calType}`
  );
}

export function getGameHandicapTotalScoreRankData(gameId: string) {
  return axios.get<HttpResponse<HandicapTotalScoreRankUser[]>>(
    `/api/v1/admin/game/rank/total-score?gameId=${gameId}`
  );
}

export function getGameHandicapPureScoreRankData(gameId: string) {
  return axios.get<HttpResponse<HandicapPureSoreRankUser[]>>(
    `/api/v1/admin/game/rank/pure-score?gameId=${gameId}`
  );
}
